package cn.regexp.seckill.mq;

import cn.regexp.seckill.web.controller.OrderInfoController;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Regexpei
 * @date 2024/7/5 23:55
 * @description 修改本地标识监听器
 */
@Component
@RocketMQMessageListener(consumerGroup = "MQCancelLocalFlagListener", topic =
        MQConstant.CANCEL_SECKILL_OVER_SIGE_TOPIC, messageModel = MessageModel.BROADCASTING    // 设置为广播模式
)
public class MQCancelLocalFlagListener implements RocketMQListener<Long> {

    @Override
    public void onMessage(Long seckillId) {
        // 获取本地标识存放的 map
        ConcurrentHashMap<Long, Boolean> map = OrderInfoController.seckillProductStock;
        // 判断 map 中是否存在该秒杀商品 id
        if (map.containsKey(seckillId)) {
            // 将本地标识设置为 false，表示商品还有库存
            map.put(seckillId, false);
        }
    }
}
